//
//  MSVerifyCell.m
//  MOSS
//
//  Created by apple on 2019/1/9.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "MSVerifyCell.h"

@interface MSVerifyCell ()<UITextFieldDelegate>

@end

@implementation MSVerifyCell

+ (instancetype)createTableView:(UITableView *)tableView {
    MSVerifyCell *cell   = [tableView dequeueReusableCellWithIdentifier:@"verifyCell"];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;

    [cell makeViews];
    
    return cell;
}

- (void)verifyClick {
    if (self.verifyBlock) {
        self.verifyBlock(self.verifyBtn);
    }
}


- (void)makeViews {
    
    [self.contentView addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(25);
        make.right.mas_equalTo(-120);
        make.height.mas_equalTo(30);
    }];
    
    
    [self.contentView addSubview:self.verifyBtn];
    [self.verifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.width.mas_equalTo(80);
        make.right.mas_equalTo(-40);
        make.height.mas_equalTo(30);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(40);
        make.right.equalTo(-40);
        make.top.equalTo(59.5);
        make.height.equalTo(0.5);
    }];
    lineView.backgroundColor = tTableViewCellLine;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = textField.text;
    if (text.length >= 6) {
        if (string.length != 0) {
            return NO;
        }
    }
//    BOOL res = [MSTool isNum:string];
//    if (!res) {
//        return NO;
//    }
    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSString *text = textField.text;
    
    if (text.length < 4 || text.length > 6 ) {
        //[SVProgressHUD showErrorWithStatus:@"验证码格式不正确"];
    }
    
    return YES;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.delegate    = self;
        _textField.textColor   = tThreeColor;
        _textField.placeholder = LocalizationKey(@"Please enter verification code");
//        [_textField setValue:[UIColor colorWithHEX:@"f6f6f6"] forKeyPath:@"_placeholderLabel.textColor"];
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    
    return _textField;
}

- (UIButton *)verifyBtn {
    if (!_verifyBtn) {
        _verifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _verifyBtn.tag = 100;
        _verifyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_verifyBtn setTitle:LocalizationKey(@"sendValidate") forState:UIControlStateNormal];
        [_verifyBtn setTitleColor:tMainColor forState:UIControlStateNormal];
        [_verifyBtn addTarget:self action:@selector(verifyClick) forControlEvents:UIControlEventTouchUpInside];
        
//        [_verifyBtn setOriginaStyle];
    }
    
    return _verifyBtn;
}

@end
